<?php

namespace app\Admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Login extends Controller
{

    public function jumps()
    {
        return view('login');
    }

    public function login()
    {
        $arr = [];
        $arr['username'] = input('username');
        $arr['pwd'] = input('pwd');
        $code = input('code');
        $result = $this->validate(
            [
                'username' => input('username'),
                'pwd' => input('pwd'),
                'code' => input('code')
            ],
            [
                'username' => 'require',
                'pwd' => 'require',
                'code' => 'require'
            ]);
        if (true !== $result) {
            return json(['code'=>300,'data'=>'','msg'=>$result]);
        }
        if (!captcha_check($code)) {
           return json(['code'=>300,'data'=>'','msg'=>'验证码错误']);
        };
        $user = Db::table('user')->where($arr)->find();
        if($user){
            Session::set('uid',$user['uid']);
            return json(['code'=>200,'data'=>$user,'msg'=>'登录成功']);
        }else{
            return json(['code'=>300,'data'=>'','msg'=>'登录失败']);
        }
    }
}
